<template>
	<view>
		<cu-custom bgColor="bg-gradual-pink" :isBack="(curPage > 1)" :isBackHome="(curPage <= 1)">
			<block slot="content">登录</block>
		</cu-custom>
		<form @submit="submitForm">
			<view class="padding-xs flex align-center">
				<view class="flex-sub text-center">
					<view class="text-xsl padding">
						<view class="cu-avatar xl round margin-left bg-grey">
							<uni-text class="avatar-text"><span class="cuIcon-my"></span></uni-text>
						</view>
					</view>
				</view>
			</view>			
			<view class="cu-form-group">
				<view class="title">帐号</view>
				<input placeholder="请输入帐号" name="input" v-model="username"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">密码</view>
				<input placeholder="请输入密码" name="input" :password="true" v-model="passwd"></input>
			</view>
			<view class="padding flex flex-direction">
				<button class="cu-btn bg-blue margin-tb-sm lg round" :disabled="formDisabled" form-type="submit">确 定</button>
			</view>
		</form>
	</view>
</template>

<script>
	import formatData from '@/common/formData.js';
	import graceChecker from '@/common/graceChecker.js';
	export default {
		data() {
			return {
				username:'',
				curPage:0,
				passwd:'',
				countDown:0,
				startDown:60,
				formDisabled:false,
				codeDisabled:false,
				rules:[
					{name:'username',errorMsg:'请输入用户名',checkType:'phoneno'},
				],
				mcode:'',
			}
		},
		onLoad(query) {
			// 获取是否页面有推荐码进来
			console.log('query=>',query);
			if(query.q){
				let url = decodeURIComponent(query.q);
				let getParams = formatData.getQuery(url);
				console.log('getParams=>',getParams);
				this.mcode = getParams.code;
			}
			
			if(query.mcode){
				this.mcode = query.mcode;
			}
		},
		onShow(){
			let curPage = getCurrentPages();
			this.curPage = curPage.length;
			console.log({curPage});
		},
		beforeMount(){
			var _this = this;
			//获取用户信息，openid
			formatData.getUserinfo(function(user){
				// console.log({user});
			});
		},
		mounted() {
			
			
		},
		methods: {
			submitForm(e) {
				uni.showLoading({
					mask:true,
					title:'登录中',
				});
				
				//提交登录
				//请求发送短信
				var params = {
					method:'login.by.username',
					username: this.username,
					passwd: this.passwd,
				};
				
				formatData.set(params);
				uni.request({
					url: formatData.httpUrl(),
					data:params,
					method:'POST',
					header: {
					  'content-type': 'application/x-www-form-urlencoded'
					},
					success: (res) => {
						var result = res.data.data;
						if(result.msg){
							setTimeout(function(){
								uni.showToast({
									icon:'none',
									title:result.msg,
									duration:2000,
									mask:true
								});
							},200);
						}						
						
						//成功后处理
						if(result.success){
							//刷新本地的缓存数据
							formatData.setUserinfoStorage(result.userinfo);
							//跳转首页
							setTimeout(function(){
								uni.reLaunch({
									url: '/pages/index/index'
								});
							},1500);
						}	
					},
					complete(res) {
						//隐藏载入效果
						uni.hideLoading();
					},
				});
			}
		}
	}
</script>
<style>
	.cu-avatar.xl{width: 90px;height: 90px;}
</style>